﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Com.Toook.Common;
using System.Data.OleDb;

public partial class nst_article_ArticleList : BasePage
{
    protected string PageHtml = "";
    private int pageSize = 10;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitDropDownListType();
            GetListData();
        }
    }

    private void GetListData()
    {
        string page = txtCurrentPage.Text;
        int p = 0;
        if (string.IsNullOrEmpty(page) || !int.TryParse(page, out p))
        {
            page = "1";
            p = 1;
            txtCurrentPage.Text = "1";
        }
        int type = int.Parse(ddlType.SelectedValue);
        int totalCount = PageData.GetTotalCount(type);
        string typePageIdConditon = PageData.GetPageStartEndCondition(type, (p - 1) * pageSize + 1, pageSize);

        string sql = "select id, title, typePageId, author, sourceSite, createTime, score, clickCount, likeCount, totalPage " +
            " from PublishArticle where " + typePageIdConditon;

        OleDbConnection con = AccessHelper.GetConnection();
        OleDbCommand cmd = new OleDbCommand(sql, con);
        OleDbDataReader reader = null;
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            rptList.DataSource = reader;
            rptList.DataBind();
        }
        catch (Exception ex)
        {
            LogHelper.Log.Error("读取文章列表出错，condition=" + typePageIdConditon, ex);
            lblTotalCount.Text = "读取文章列表出错，condition=" + typePageIdConditon + ", " + ex.Message;
        }
        finally
        {
            AccessHelper.CloseDbResource(con, reader);
        }

        int pageCount = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;//总页数
        lblTotalCount.Text = totalCount.ToString();
        lblTotalPage.Text = pageCount.ToString();
    }

    private void InitDropDownListType()
    {
        DataRow[] rows = PageData.SelectParamRows("parentId=60");
        foreach (DataRow row in rows)
        {
            ddlType.Items.Add(new ListItem(row["typeName"].ToString(), row["typeId"].ToString()));
        }
    }
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetListData();
    }
    protected void ButtonGotoPage_Click(object sender, EventArgs e)
    {
        GetListData();
    }
}

